﻿using UnityEngine;
using System.Collections;

public delegate void OnFire();

public class Player : MonoBehaviour 
{
    public OnFire OnFireHandler;
    public float fireRate = 0.1f; //bullet per second
    public float movementSpeed = 2;
    public float jumpHeight = 1;

    private int xDirection;
    private int yDirection;
    private float bulletFireTime = 0f;

    private Vector3 position
    {
        get { return transform.position; }
        set { transform.position = value;}
    }

    private Vector3 prevPosition;
    private Quaternion rotation;
    private Animator animator;
    
    private ACTION_STATES previousState;
    private ACTION_STATES currentState = ACTION_STATES.NONE;

    public ACTION_STATES State
    {
        get { return currentState; }
        set
        {
            if (currentState == value)
                return;
            previousState = currentState;
            currentState = value;

            switch (currentState)
            {
                case ACTION_STATES.IDLE:
                    animator.SetBool("IsMoving", false);
                    animator.SetBool("IsJumping", false);
                    break;
                case ACTION_STATES.WALK:
                    animator.SetBool("IsMoving", true);
                    animator.SetBool("IsJumping", false);
                    animator.SetBool("IsFiring", false);
                    break;
                case ACTION_STATES.JUMP:
                     animator.SetBool("IsMoving", false);
                    animator.SetBool("IsJumping", true);
                    animator.SetBool("IsFiring", false);
                    break;
            }
        }
    }
    public enum DIRECTIONS
    {
        RIGHT,
        LEFT,
        UP,
        DOWN,
        NONE
    }

    public enum ACTION_STATES
    {
        NONE,
        IDLE,
        WALK,
        JUMP,
        MELEE_HIT,
        FIRE,
        DEAD
    }


	// Use this for initialization
	void Start () {
        position = transform.position;
        rotation = transform.rotation;
        animator = GetComponent<Animator>();
        Init();
	}
	
    public void Init()
    {
        State = ACTION_STATES.IDLE;

        xDirection = (int)DIRECTIONS.RIGHT;
        yDirection = (int)DIRECTIONS.NONE;
  
    }

	// Update is called once per frame
	void Update () {

        UpdateState();
	}

    void UpdateState()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        bool jumpInput = Input.GetKeyDown(KeyCode.UpArrow);
        bool meleeInput = Input.GetKeyDown(KeyCode.Space);
        bool fireInput = Input.GetKey(KeyCode.Z);

        if (horizontalInput > 0.0f || horizontalInput < 0.0f)
        {
            if (State != ACTION_STATES.JUMP)
                State = ACTION_STATES.WALK;

            position = new Vector3(position.x + movementSpeed * Time.deltaTime * (horizontalInput > 0.0f ? 1.0f : -1.0f), position.y, position.z);
            xDirection = (int)(horizontalInput > 0.0f ? DIRECTIONS.RIGHT : DIRECTIONS.LEFT);
        }
        else if (State == ACTION_STATES.WALK)
            State = ACTION_STATES.IDLE;

        if (jumpInput && State != ACTION_STATES.JUMP)
        {
            State = ACTION_STATES.JUMP;
            yDirection = (int)DIRECTIONS.UP;
            prevPosition = position;
        }

        if (fireInput)
        {
            if (State == ACTION_STATES.IDLE)
                animator.SetBool("IsFiring", true);

            bulletFireTime += Time.deltaTime;

            if (OnFireHandler != null && bulletFireTime > fireRate)
            {
                OnFireHandler();
                bulletFireTime = 0;
            }
        }
        else
        {
            animator.SetBool("IsFiring", false);
            bulletFireTime = fireRate;
        }

        if (State != ACTION_STATES.WALK && State != ACTION_STATES.JUMP)
            State = ACTION_STATES.IDLE;

        if (State == ACTION_STATES.JUMP)
        {
            if (yDirection == (int)DIRECTIONS.UP)
            {
                if (position.y < prevPosition.y + jumpHeight)
                    position = new Vector3(position.x, position.y + movementSpeed * Time.deltaTime, position.z);
                else
                {
                    yDirection = (int)DIRECTIONS.DOWN;
                }
            }

            if (yDirection == (int)DIRECTIONS.DOWN)
            {
                if (position.y >= prevPosition.y)
                    position = new Vector3(position.x, position.y - movementSpeed * Time.deltaTime, position.z);
                else
                    State = ACTION_STATES.IDLE;
            }
        }
    }
}
